package com.hyhy.hycloud.commons.constant;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @author lanpengfei
 * @date 2018/12/7
 */
@AllArgsConstructor
public enum AgeUnit {
    /**
     *
     */
    DAY("D", 1),
    WEEK("W", 2),
    MONTH("M", 3),
    YEAR("Y", 4);

    @Getter
    private String code;

    @Getter
    private Integer value;

    public static AgeUnit parse(String code) {
        for (AgeUnit ageUnit : AgeUnit.values()) {
            if (ageUnit.getCode().equalsIgnoreCase(code) || ageUnit.name().equalsIgnoreCase(code)) {
                return ageUnit;
            }
        }
        throw new UnsupportedOperationException();
    }

    public static AgeUnit valueOf(Integer value) {
        switch (value) {
            case 1:
                return DAY;
            case 2:
                return WEEK;
            case 3:
                return MONTH;
            case 4:
                return YEAR;
            default:
                throw new UnsupportedOperationException();
        }
    }
}
